package com.czy.booktest02.demos.service.impl;

import com.czy.booktest02.demos.common.dtos.ResponseResult;
import com.czy.booktest02.demos.common.enums.AppHttpCodeEnum;
import com.czy.booktest02.demos.common.redis.CacheService;
import com.czy.booktest02.demos.common.redis.RedisKey;
import com.czy.booktest02.demos.mapper.BookMapper;
import com.czy.booktest02.demos.mapper.ShoppingCartMapper;
import com.czy.booktest02.demos.pojo.bo.AccountUserBo;
import com.czy.booktest02.demos.pojo.dto.CartDto;
import com.czy.booktest02.demos.pojo.po.Book;
import com.czy.booktest02.demos.pojo.po.Books;
import com.czy.booktest02.demos.pojo.po.ShoppingCart;
import com.czy.booktest02.demos.pojo.vo.BookVO;
import com.czy.booktest02.demos.pojo.vo.CartVO;
import com.czy.booktest02.demos.service.ShoppingCartService;
import com.czy.booktest02.demos.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    CacheService cacheService;
    @Override
    public ResponseResult addCart(CartDto cartDto) {
        ShoppingCart shoppingCart = BaseUtils.copyProperties(cartDto, ShoppingCart.class);
        shoppingCart.setUserId(AccountUserBo.getUser().getUserId());
        if (shoppingCartMapper.addBookToCart(shoppingCart)>0){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult addOrDecBook(CartDto cartDto) {
        ShoppingCart shoppingCart = BaseUtils.copyProperties(cartDto, ShoppingCart.class);
        shoppingCart.setUserId(AccountUserBo.getUser().getUserId());
        if(cartDto.getQuantity()>0) {
            //增加
            if (shoppingCartMapper.addOrDecBook(shoppingCart) > 0) {
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        } else if (cartDto.getQuantity()==0) {
            //删除
            if(shoppingCartMapper.deleteBookFromCart(shoppingCart.getId())>0){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }

    @Override
    public ResponseResult showCart() {
        //根据用户id查询所有购物项
        Integer userId = AccountUserBo.getUser().getUserId();
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectListById(userId);
        List<CartVO> cartList = new ArrayList<>();
        if(shoppingCarts!=null) {

            for (ShoppingCart shoppingCart : shoppingCarts) {
                CartVO cartVO = BaseUtils.copyProperties(shoppingCart, CartVO.class);
                Books book = bookMapper.selectId(shoppingCart.getBookId());
                cartVO.setBookName(book.getTitle());
                cartList.add(cartVO);
            }
        }
        Map<String,Object> bookStock=new HashMap<>();
        for (Books book : bookMapper.selectStock()) {
            bookStock.put(String.valueOf(book.getId()),book.getStock());
        }

        cacheService.setMap(RedisKey.BOOK_STOCK,bookStock);
        return ResponseResult.okResult(cartList);
    }

    @Async
    @Override
    public ResponseResult deleteCartItem(List<Integer> ids) {
        if(ids!=null) {
            for (Integer id : ids) {
                shoppingCartMapper.deleteId(id);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
    }
}
